package ru.tulupov.quadraticequation;

import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

/**
 * Quadratic equation parser
 * 
 * @author <a href="mailto:zhekka3@gmail.com">Eugene Tulupov</a>
 */
public class QuadraticEquationParser {
	private Scanner in;

	/**
	 * Constructs parser using input stream.
	 * 
	 * @param is
	 *            the input stream.
	 */
	public QuadraticEquationParser(InputStream is) {
		this.in = new Scanner(is);
	}

	/**
	 * Parse quadratic equation from stream.
	 * 
	 * @return the quadratic equation object.
	 * @throws ParseException
	 * @throws EOFParseException
	 */
	public QuadraticEquation parse() {
		QuadraticEquation equation = null;

		try {
			equation = new QuadraticEquation(in.nextDouble(), in.nextDouble(),
					in.nextDouble());
		} catch (InputMismatchException e) {
			// skip invalid token
			in.next();
			throw new ParseException();
		} catch (NoSuchElementException e) {
			throw new EOFParseException();
		}

		return equation;
	}
}
